import {
	getOpenId
} from '../../utils/getOpenid.js'
import {
	get,
	post
} from '../../utils/request.js'
let WxParse = require('../../utils/wxParse/wxParse.js')
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		goodsid: '',
		openid: '',
		swiper: [],
		info: {},
		attribute: [],
		goods_desc: '',
		issueList: [],
		productList: [],
		topIconIsShow: false, // 控制滚动到top的icon的显示
		isShowStar: false,
		overLayShow: false,
		number: 1,
		isBuy: false,
		allCartNumber: 0,
		allPrice: 0,
		bygoodsId: '',
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		// console.log(options)
		getOpenId()
		this.setData({
			openid: wx.getStorageSync('OPENID'),
			goodsid: options.id
			// goodsid: '1009024'
		})
		// console.log(this.data.openid, this.data.goodsid)
		this.goodsDetail()
		// console.log(this.data.goods_desc)
	},
	async goodsDetail() {
		const data = await get('/goods/detailAction', {
			id: this.data.goodsid,
			openId: this.data.openid,
		})
		// console.log(data)
		this.setData({
			gallery: data.gallery,
			info: data.info,
			attribute: data.attribute,
			goods_desc: data.info.goods_desc,
			issueList: data.issueList,
			productList: data.productList,
			isShowStar: data.collected,
			allCartNumber: data.allnumber,
			allPrice: data.info.retail_price,
			bygoodsId: data.info.id
		})
		let des = this.data.goods_desc
		var that = this
		WxParse.wxParse('des', 'html', des, that)
	},
	goToDetail(e) {
		let id = e.currentTarget.dataset.id
		wx.navigateTo({
			url: `/pages/goodsdetail/goodsdetail?id=${id}`
		})
	},
	async addStar() {
		let isShowStar = this.data.isShowStar
		// console.log(isShowStar)
		this.setData({
			isShowStar: !isShowStar
		})
		// console.log(this.data.info)
		const data = await post('/collect/addCollect', {
			openId: wx.getStorageSync('OPENID') || '',
			goodsId: this.data.goodsid,
			info: this.data.info.name
		})
		wx.showToast({
			title: '操作成功',
			duration: 2000,
			icon: 'success'
		})
		// console.log(data)
	},
	goToComment() {
		wx.switchTab({
			url: `/pages/comment/comment`
		})
	},
	goToCart() {
		wx.switchTab({
			url: `/pages/cart/cart`
		})
	},
	toAddCart () {
		this.setData({
			overLayShow: true,
			isBuy: false
		})
	},
	toBuy () {
		this.setData({
			overLayShow: true,
			isBuy: true
		})
	},
	onChange (e) {
		// console.log(e)
		this.setData({
			number: e.detail
		})
	},
	async confirmBuy (e) {
		const data = await post('/order/submitAction', {
		  goodsId: this.data.bygoodsId,
		  openId: this.data.openid,
		  allPrice: this.data.allPrice
		})
		// console.log(e.currentTarget.dataset.info)
		// console.log(data)
		if (data.data) {
			wx.setStorageSync('INFO', e.currentTarget.dataset.info)
			wx.setStorageSync('INFONUMBER', this.data.number)
			wx.navigateTo({
				url: `/pages/settlepage/settlepage`
			})
		}
	},
	async confirmAddCart () {
		// console.log(this.data.number)
		const data = await post('/cart/addCart', {
			openId: wx.getStorageSync('OPENID') || '',
			goodsId: this.data.goodsid,
			number: this.data.number
		})
		if (data) {
			this.data.allCartNumber = this.data.allCartNumber + this.data.number
			wx.startPullDownRefresh()
			let that = this
			wx.showToast({
				title: '添加成功',
				duration: 1500,
				icon: 'success',
				success() {
					that.setData({
						overLayShow: false
					})
				}
			})
			wx.stopPullDownRefresh()
		}
		// console.log(data, this.data.allCartNumber)
	},
	closePop () {
		this.setData({
			overLayShow: false
		})
	},
	onShareAppMessage() {
		return {
			title: this.data.info.name, // 标题
			path: '/pages/goods/main?id' + this.data.info.id, // 那个页面当做 跳转的页面
			imageUrl: this.data.gallery[0].img_url // 分享页图片 不设置会自动截屏
		}
	},
	onPreviewImage(event) { // 实现浏览图片
		const data = event.target.dataset.src
		// console.log(data)
		wx.previewImage({
			current: data,
			urls: [data]
		})
	},
	onPageScroll(e) { // page页面的监听滚动的事件
		if (e.scrollTop > 100) { // 如果滚动大于100 就显示top组件
			this.setData({
				topIconIsShow: true
			});
		} else {
			this.setData({
				topIconIsShow: false
			});
		}
	},
	onPullDownRefresh() { // 下拉刷新
		// console.log(this.data.allCartNumber)
		this.setData({
			allCartNumber:this.data.allCartNumber
		})
	},
})
